<?php
/**
 * 路由配置
 * @copyright (c)2011-2012 dxphp.com
 * @license   dxtoo.cn
 * @author    lnbalife@126.com
 * @version   1.0beta
 */
class DRoute extends DComponent
{
	public function __construct(){
		if(!get_magic_quotes_gpc()) {
			$_POST = cxaddslashes($_POST);
			$_GET = cxaddslashes($_GET);
			$_REQUEST = cxaddslashes($_REQUEST);
			$_COOKIE = cxaddslashes($_COOKIE);
		}
	}
	
	public function init(){
		$dm = mc('route.defaultModule') ? mc('route.defaultModule') : 'index';
		$dc = mc('route.defaultController') ? mc('route.defaultController') : 'default';
		$da = mc('route.defaultAction') ? mc('route.defaultAction') : 'index';
		
		if(mc('route.url_type') == 'catalog'){
			$path = $this->get_params(mc('route.catalog.route_var'));
			if($path[0]=='/') $path=substr($path,1);
			$path=explode('/',$path); 
			$this->route_curr['module']     = $path[0] ? $path[0] : $dm; 
			$this->route_curr['controller'] = (!empty($path[1])) ? $path[1] : $dc; 
			$this->route_curr['action']     = (!empty($path[2])) ? $path[2] : $da;
		}elseif(mc('route.url_type') == 'params'){
			$route_m = mc('route.params.route_var.module') ? mc('route.params.route_var.module') : 'm';
			$route_c = mc('route.params.route_var.control') ? mc('route.params.route_var.control') : 'c';
			$route_a = mc('route.params.route_var.action') ? mc('route.params.route_var.action') : 'a';
			
			$this->route_curr['module']     = $this->get_params($route_m) ? $this->get_params($route_m) : $dm;
			$this->route_curr['controller'] = $this->get_params($route_c) ? $this->get_params($route_c) : $dc;
			$this->route_curr['action']     = $this->get_params($route_a) ? $this->get_params($route_a) : $da;
		}else{
			$this->route_curr['module']     = $dm; 
			$this->route_curr['controller'] = $dc; 
			$this->route_curr['action']     = $da;
		}
		
		if(isset($_GET['page'])) $_GET['page'] = max(intval($_GET['page']),1);
		return true;
	}
	
	/**
	 * 获取模型
	 */
	public function route_m() {
		return $this->route_curr['module'];
	}
	
	/**
	 * 获取控制器
	 */
	public function route_c() {
		return $this->route_curr['controller'].'Control';
	}

	/**
	 * 获取事件
	 */
	public function route_a() {
		return 'action'.ucfirst($this->route_curr['action']);
	}
	
	/**
	 * 获取参数值
	 * @param unknown_type $vars
	 */
	public function get_params($vars){
		$params = isset($_GET[$vars]) && !empty($_GET[$vars]) ? $_GET[$vars] : (isset($_POST[$vars]) && !empty($_POST[$vars]) ? $_POST[$vars] : '');
		return $params;
	}
}
?>